package com.resou.reader.reading;

import android.os.Looper;
import android.support.constraint.ConstraintLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import com.glong.reader.widget.ReaderView;
import com.resou.reader.R;
import com.resou.reader.api.entry.BookContentBean;
import com.resou.reader.api.entry.ChapterItemBean;
import com.resou.reader.api.entry.Result;
import com.resou.reader.data.Injection;
import com.resou.reader.utils.AppExecutors;
import com.resou.reader.utils.EncryptUtils;
import com.resou.reader.utils.UserUtil;

import java.io.IOException;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Call;

public class ReadingAdapter extends ReaderView.Adapter<ChapterItemBean, BookContentBean> {
    private ReaderView mReaderView;

    public ReadingAdapter(ReaderView readerView) {
        mReaderView = readerView;
    }

    @Override
    public String obtainCacheKey(ChapterItemBean chapterItemBean) {
//        String isPay = chapterItemBean.getIsPay();
//        if ("0".equals(isPay)) {
//            AppExecutors.getAppExecutors().mainThread().execute(() -> {
//                View childView = LayoutInflater.from(mReaderView.getContext()).inflate(R.layout.chapter_pay_view, null);
//                mReaderView.addView(childView,ReaderView.ChildInPage.FIRST_PAGE);
//            });
//        }
        return chapterItemBean.getChapterId();
    }

    @Override
    public String obtainChapterName(ChapterItemBean chapterItemBean) {
        return chapterItemBean.getChapterName();
    }

    private static final String TAG = "ReadingAdapter";

    @Override
    public String obtainChapterContent(BookContentBean bookContentBean) {

        try {
//            return EncryptUtils.decrypt(bookContentBean.getContent(), bookContentBean.getBookId(), bookContentBean.getChapterId());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public boolean isMainThread() {
        return Looper.getMainLooper() == Looper.myLooper();
    }

    @Override
    public BookContentBean downLoad(ChapterItemBean chapterItemBean) {
        Log.d(TAG, "downLoad:  isMainThread" + String.valueOf(isMainThread()));
//        OkHttpClient client = Injection.provideOkHttpClient();
//        String url = "https://rs.resouxs.com/book/reading?" + "bookId" + chapterItemBean.getBookId() + "chapterId" + chapterItemBean.getChapterId();
//
//        Request request = new Request.Builder()
//                .url(url)
//                .addHeader("token", UserUtil.getToken())
////                .post("bookId", chapterItemBean.getBookId())
////                .addUrlParams("chapterId", chapterItemBean.getChapterId())
//                .build();
//        try {
//            Response response = client.newCall(request).execute();
//            if (response.isSuccessful()) {
//
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//            return null;
        String token = UserUtil.getToken();
        Call<Result<BookContentBean>> resultCall = Injection.provideBookService().getBookContentV2Call(token, chapterItemBean.getBookId(), chapterItemBean.getChapterId());
        try {
            retrofit2.Response<Result<BookContentBean>> response = resultCall.execute();
            Log.d(TAG, "downLoad: " + response.isSuccessful());
            if (response.isSuccessful()) {
                if (response.body() != null) {
                    return response.body().getData();
                }
            } else {
                Log.d(TAG, "downLoad: " + response.isSuccessful());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

//    @Override
//    public Request requestParams(ChapterItemBean chapterItemBean) {
//        return new Request.Builder().baseUrl("https://rs.resouxs.com/book/reading")
//                .addHeader("token", UserUtil.getToken())
//                .addUrlParams("bookId", chapterItemBean.getBookId())
//                .addUrlParams("chapterId", chapterItemBean.getChapterId())
//                .post().build();
//    }
}
